﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;

using FluentNHibernate.Mapping;

namespace SmartProject.Mapping
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("SmartProject_User");
            Id(x => x.Id).Column("UserId");
            Map(x => x.Username).Length(200).Unique().Not.Nullable();
            Map(x => x.Uid).Length(10).Not.Nullable();
            Map(x => x.LastLogin).Nullable();
            Map(x => x.LastLogout).Nullable();
            Map(x => x.FailedLoginCount).Nullable();
            Map(x => x.FirstName).Length(150).Not.Nullable();
            Map(x => x.LastName).Length(150).Not.Nullable();
            Map(x => x.Email).Length(250).Unique().Not.Nullable();
            HasMany<Message>(x => x.ReceivedMessages)
                .KeyColumn("RecipientId")
                .LazyLoad();
            HasMany<Message>(x => x.SentMessages)
                .KeyColumn("OwnerId")
                .LazyLoad();
            HasMany<Profile>(x => x.Profiles)
                .KeyColumn("OwnerId")
                .Cascade.All()
                .LazyLoad();
            HasManyToMany<Role>(x => x.Roles)
                .Table("SmartProject_UserRoles")
                .ParentKeyColumn("UserId")
                .ChildKeyColumn("RoleId")
                .Cascade.All()
                .Fetch.Select();
            Map(x => x.Password).Not.Nullable().LazyLoad();
            Map(x => x.Active).Not.Nullable();
            Map(x => x.Created).Not.Nullable();
        }
    }
}
